//
//  BMHWithdrawalRecordVController.m
//  BMHVideoMain
//
//  Created by 爆米花 on 2017/8/9.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "BMHWithdrawalRecordVController.h"
#import "BMHYearMonthDatePickerView.h"
#import "BMHWithdrawalTableViewCell.h"
#import "BMHWithdrawalListModel.h"
#import <ReactiveCocoa.h>
#import "XLHTTPSessionManager.h"

#import "XLUserInfoModel.h"
#import "XLAccountManager.h"
#import "BMHDatePickerView.h"
#import "LQCActionSheet.h"

@interface BMHWithdrawalRecordVController ()<UITableViewDelegate,UITableViewDataSource,LQCActionSheetDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) BMHDatePickerView *datePickerView;
@property (nonatomic, strong) BMHYearMonthDatePickerView *yearMonthDatePickerView;

@property (nonatomic, copy) NSString *yearStr;
@property (nonatomic, copy) NSString *moneyStr;
@property (nonatomic, strong) LQCActionSheet *actionSheet;

@end

@implementation BMHWithdrawalRecordVController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
        
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showBackButton:NULL];
    self.title = @"提现记录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"me_money_bill"] style:UIBarButtonItemStyleDone target:self action:@selector(monthlyWithdrawaAction)];

    
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    [formatter setDateFormat:@"yyyy"];
    self.yearStr = [formatter stringFromDate:currentDate];
    [formatter setDateFormat:@"MM"];
    self.moneyStr = [formatter stringFromDate:currentDate];
    
    [self addTableView];
    
    [self withdrawallistData];
}

- (void)addTableView {
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
    topView.backgroundColor = XLThemeColor;
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.textColor = [UIColor whiteColor];
    _timeLabel.text = [NSString stringWithFormat:@"%@年%@月",self.yearStr, self.moneyStr];
    _timeLabel.font = [UIFont systemFontOfSize:14];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = XLLineColor;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = XLBgColor;
    [self.tableView registerClass:[BMHWithdrawalTableViewCell class] forCellReuseIdentifier:NSStringFromClass([BMHWithdrawalTableViewCell class])];
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = topView;
    [topView addSubview:_timeLabel];
    
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topView);
        make.left.equalTo(topView).offset(15);
        make.height.equalTo(@15);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
}


- (void)withdrawallistData {
    @weakify(self);
    _timeLabel.text = [NSString stringWithFormat:@"%@年%@月",self.yearStr, self.moneyStr];
    NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:UserID];
    [[[XLHTTPSessionManager sharedManager] canCashOutIsAuthorizeRecordWithUserId:uid andYear:[self.yearStr intValue] andMonth:[self.moneyStr intValue] pageIndex:0 andSign:[[XLAccountManager shareInstance] model].SecretKey] subscribeNext:^(id x) {
        @strongify(self);
        
        
        NSArray * listAry = x;
        if (listAry.count > 0) {
            
            [self getJsonArray:listAry];
        }else{
            [MBProgressHUD showContent:@"无数据" toView:self.view];
        }
        
        
    } error:^(NSError *error) {
        if (error.code == 1001) {
            [MBProgressHUD showContent:@"验签失败" toView:self.view];
        }else if (error.code == 1002){
            [MBProgressHUD showContent:@"无数据" toView:self.view];
            [self.dataSource removeAllObjects];
            [self.tableView reloadData];
            
        }else if (error.code == 1003){
            
            [MBProgressHUD showContent:@"请求参数错误" toView:self.view];
        }else if (error.code == 1004){
            [MBProgressHUD showContent:@"票证已过期" toView:self.view];
        }
        
    }];
    
}

//- (void)getJsonArray:(NSArray *)array {
//    if ([array isKindOfClass:[NSNull class]] || array.count == 0 || array == nil) {
//
//    } else {
//        [self.dataSource removeAllObjects];
//
//        [self.dataSource addObjectsFromArray:array];
//    }
//
//    [self.tableView reloadData];
//
//
//}

- (void)getJsonArray:(NSArray *)array {
    [self.dataSource removeAllObjects];
    if (array.count == 0) {
        
    }else{

        [self.dataSource addObjectsFromArray:array];
        
    }
    [_tableView reloadData];
    
    
}

- (void)monthlyWithdrawaAction {
    
    [self.actionSheet close];
    self.yearMonthDatePickerView = [[BMHYearMonthDatePickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 162 + 100)];
    LQCActionSheet *actionSheet = [[LQCActionSheet alloc] initWithHeight:self.yearMonthDatePickerView.height isNeedPerspective:NO];
    [actionSheet.contentView addSubview:self.yearMonthDatePickerView];
    actionSheet.delegate = self;
    self.actionSheet = actionSheet;
    [self.view addSubview:actionSheet];
    
    __weak BMHWithdrawalRecordVController *bmhMonth = self;
    
    self.yearMonthDatePickerView.selectedYearMonth = ^(NSString *year, NSString *month) {
        [actionSheet close];
        bmhMonth.yearStr = year;
        bmhMonth.moneyStr = month;
        
        [bmhMonth withdrawallistData];
        
    };
}

#pragma mark - QXActionSheetDelegate
-(void)actionSheetWillClose:(LQCActionSheet *)actionSheet{
    if (self.datePickerView) {
        [self.datePickerView removeFromSuperview];
        self.datePickerView = nil;
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BMHWithdrawalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BMHWithdrawalTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = XLThemeColor;
    BMHWithdrawalListModel *model = (BMHWithdrawalListModel *)self.dataSource[indexPath.row];
    cell.withdrawalModel = model;
    
    return cell;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
